import { Body, Controller, Delete, Get, HttpService, Post, Put, Query, Req, Res } from '@nestjs/common';
import { Response } from 'express';
import { ApiResult, ApiResultCode } from '../../common/api-result';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { PluginAliyunSLB } from './plugin-aliyun-slb.entity';
import { Page } from '../../common/page';
import { EnvDto } from '../../env/env.dto';
import { ServerDto } from '../../server/server.dto';

@Controller()
export class PluginAliyunSLBController {

  constructor(
    @InjectRepository(PluginAliyunSLB)
    private pluginAliyunSLBRepository: Repository<PluginAliyunSLB>,
  ) {
  }

  /**
   * 添加阿里云SLB
   * @param dto
   * @param res
   */
  @Post('/api/plugin/aliyun-slb/save')
  async save(@Body() dto: PluginAliyunSLB, @Res() res: Response) {
    const ar = new ApiResult();
    if (!dto.instanceId) {
      return res.json(ApiResultCode['2']('instanceId 不能为空'));
    }
    let slb = await this.pluginAliyunSLBRepository.findOne({ instanceId: dto.instanceId });
    if (!slb) {
      slb = new PluginAliyunSLB();
    }
    slb.instanceId = dto.instanceId;
    slb.instanceName = dto.instanceName;
    slb.isMonitor = dto.isMonitor;
    await this.pluginAliyunSLBRepository.save(slb);
    return res.json(ar);
  }

  /**
   * 阿里云SLB 更新监控状态
   * @param dto
   * @param res
   */
  @Put('/api/plugin/aliyun-slb/updateMonitor')
  async updateMonitor(@Body() dto: PluginAliyunSLB, @Res() res: Response) {
    const ar = new ApiResult();
    if (!dto.instanceId) {
      return res.json(ApiResultCode['2']('instanceId 不能为空'));
    }
    const slb = await this.pluginAliyunSLBRepository.findOne({ instanceId: dto.instanceId });
    if (!slb) {
      return res.json(ApiResultCode['3']('实例id 不存在'));
    }
    slb.isMonitor = dto.isMonitor;
    await this.pluginAliyunSLBRepository.save(slb);
    return res.json(ar);
  }

  /**
   * 阿里云SLB 删除
   * @param dto
   * @param res
   */
  @Delete('/api/plugin/aliyun-slb/delete')
  async delete(@Body() dto: PluginAliyunSLB, @Res() res: Response) {
    const ar = new ApiResult();
    if (!dto.instanceId) {
      return res.json(ApiResultCode['2']('instanceId 不能为空'));
    }
    const slb = await this.pluginAliyunSLBRepository.findOne({ instanceId: dto.instanceId });
    if (!slb) {
      return res.json(ApiResultCode['3']('实例id 不存在'));
    }
    await this.pluginAliyunSLBRepository.delete(slb.id);
    return res.json(ar);
  }

  @Get('/api/plugin/aliyun-slb/page')
  async list(@Query() dto: PluginAliyunSLB, @Req() req, @Res() res: Response) {
    const ar = new ApiResult();
    const page = new Page();
    await this.pluginAliyunSLBRepository.createQueryBuilder('s')
      .skip(PluginAliyunSLB.getOffset(PluginAliyunSLB.getPageNo(dto.pageNo), ServerDto.getPageSize(dto.pageSize)))
      .take(PluginAliyunSLB.getPageSize(dto.pageSize))
      .getManyAndCount()
      .then(value => {
        page.data = value[0];
        page.total = value[1];
      });
    page.pageNo = dto.pageNo;
    page.pageSize = dto.pageSize;
    // 分页总数，总记录数 / 页条数，当存在小数点，使用了 Math.ceil 直接网上取整数
    page.totalPage = Math.ceil(page.total / page.pageSize);
    ar.result = page;
    return res.json(ar);
  }

}